package com.ebt.m.update;

import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.ebt.m.R;
import com.ebt.m.utils.ConfigData;


/**
 * Created by andy.wang on 2016/10/8.
 */

public class UpdateDialogUtil {
    public UpdateDialogUtil() {
    }

    public static void showUpdateUtil(final Activity activity, final String url, final ActionListener actionListener) {
        AlertDialog.Builder dialogBuilder = (new AlertDialog.Builder(activity)).setTitle(R.string.update_title).setMessage(R.string.update_confirm_label).setNegativeButton(R.string.update_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (actionListener != null) {
                    actionListener.cancel();
                }
            }
        }).setPositiveButton(R.string.update_sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {


                UpdateDownload.Builder.create(url).setStoreDir(ConfigData.APK_UPDATE_PATH).setDownloadSuccessNotificationFlag(-1).setDownloadErrorNotificationFlag(-1).build(activity);
                dialog.dismiss();
                if (actionListener != null) {
                    actionListener.sure();
                }
            }
        }).setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (actionListener != null) {
                    actionListener.cancel();
                }
            }
        });
        dialogBuilder.show();
    }

    public interface ActionListener {
        void cancel();

        void sure();
    }

    /**
     * Returns whether notifications from the calling package are not blocked.
     */
    public void areNotificationsEnabled(Context mContext) {
        NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = manager.getNotificationChannel("chat");
            if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, mContext.getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
                mContext.startActivity(intent);
                Toast.makeText(mContext, "请手动将通知权限打开", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
